package com.thd.springboottest.s3.web;


import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.thd.springboottest.s3.config.S3Config;
import com.thd.springboottest.s3.utils.S3Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.List;

/**
 * @author devil13th

 **/
@Controller
@RequestMapping(value="/s3")
public class SssController {
    Logger log = LoggerFactory.getLogger(this.getClass());


    @ResponseBody
    @RequestMapping(value="/upload",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/s3/upload
    public ResponseEntity<String> upload() {
        this.log.info("upload");

        File f = new File("D:\\deleteme\\ts\\package.json");
        System.out.println(f.getAbsolutePath());

        InputStream input = null;
        try {
            input = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
//        s3Client.putObject(bucket.getName(),"dd.txt",input,new ObjectMetadata());

        S3Util.upload(input,"aaa/bbb/c.txt");

        return ResponseEntity.ok(" Hello RestTemplate ");
    }



    @ResponseBody
    @RequestMapping(value="/download",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/s3/download
    public ResponseEntity<String> download() {
        this.log.info("download");

        File f = new File("D:\\deleteme\\ts\\package.json");
        System.out.println(f.getAbsolutePath());

        InputStream input = null;
        try {
            input = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
//        s3Client.putObject(bucket.getName(),"dd.txt",input,new ObjectMetadata());

        S3Util.download("aaa/bbb/c.txt",new File("D://deleteme/aaaddd.txt"));

        return ResponseEntity.ok(" Hello RestTemplate ");
    }


}
